package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree.shape;

public final class IntervalShapeElement extends ShapeElement {
    public static IntervalShapeElement create(int minCardsCount, int maxCardsCount, boolean validate) {
        if (validate) {
            if (minCardsCount < 0 || minCardsCount > 13) {
                throw new InvalidShapeElementException("minCardsCount must be between 0 and 13");
            }
            if (maxCardsCount < 0 || maxCardsCount > 13) {
                throw new InvalidShapeElementException("maxCardsCount must be between 0 and 13");
            }
            if (minCardsCount >= maxCardsCount) {
                throw new InvalidShapeElementException("minCardsCount must be smaller than maxCardsCount");
            }
        }
        return new IntervalShapeElement(minCardsCount, maxCardsCount);
    }

    private int minCardsCount = 0;
    private int maxCardsCount = 13;

    IntervalShapeElement() {
    }

    /**
     * Create a new IntervalShapeElement.
     *
     * @param minCardsCount - integer between 0 and 13
     * @param maxCardsCount - integer between 0 and 13 that is bigger than minCardsCount
     */
    private IntervalShapeElement(int minCardsCount, int maxCardsCount) {
        this.minCardsCount = minCardsCount;
        this.maxCardsCount = maxCardsCount;
    }


    public int getMaxCardsCount() {
        return maxCardsCount;
    }

    public int getMinCardsCount() {
        return minCardsCount;
    }

    public boolean match(int suitCount) {
        return (minCardsCount <= suitCount && suitCount <= maxCardsCount);
    }

    public String toString() {
        return minCardsCount + ":" + maxCardsCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        IntervalShapeElement element = (IntervalShapeElement) o;
        return maxCardsCount == element.maxCardsCount && minCardsCount == element.minCardsCount;
    }

    @Override
    public int hashCode() {
        int result = minCardsCount;
        result = 31 * result + maxCardsCount;
        return result;
    }
}
